package org.datacoder.geocoding.jurisdiction

import org.datacoder.util.*

class VotingDistrictService {

    def jurisdictionService

    static transactional = true

    /* CRUD */
    public VotingDistrict create(VotingDistrict vd) throws DAOException{
        save(vd)
        return vd
    }
    public void save(VotingDistrict vd) throws DAOException{
        ServiceUtils.save(vd)
    }
    public void save(VotingDistrict vd, boolean flush) throws DAOException{
        ServiceUtils.save(vd, flush)
    }
    public void delete(VotingDistrict vd){
        if(vd == null){
            return
        }
        vd.delete(flush:true)
    }
    public VotingDistrict merge(VotingDistrict vd){
        return vd.merge()
    }
    public VotingDistrict get(String vdId){
        return get(Long.parseLong(vdId))
    }
    public VotingDistrict get(long vdId){
        return VotingDistrict.get(vdId)
    }
    public Set<VotingDistrict> getAll(){
        return VotingDistrict.findAll()
    }
    /* END CRUD */

    public VotingDistrict getByNameAndCountyOrCreate(String name, County county, String longName){
        VotingDistrict vd = getByNameAndCounty(name, county)
        if(vd == null){
            vd = new VotingDistrict(
                name:name,
                longName:longName,
                county:county
            )
            vd = create(vd)
        }
        return vd
    }

    public VotingDistrict getByNameAndCounty(String name, County county){
        VotingDistrict vd = VotingDistrict.findByNameAndCounty(name, county)
        return vd
    }
}

